<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4、k个数相加等于n的数(数字不能重复) </title>
</head>

<body>
  <script>
    var array = function (k, n) {
      const res = []
      const backtrack = (s, path, total) => {
        if (total == n && path.length == k) {
          res.push(path)
          return
        } else if (total > n) return//剪枝优化 
        for (s; s <= 9 - (k - path.length) + 1; s++) {//剪枝 优化 因为答案要存在k个数 
          backtrack(s + 1, path.concat(s), total + s)//这样的写法才是正确的 
        }
      }
      backtrack(1, [], 0)
      return res
    };
    console.log(array(3, 7))
  </script>
</body>

</html>